package com.mandala.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.mandala.LDAPConnectService;
import com.mandala.entry.Permission;
import com.mandala.entry.ResponseWrapper;
import com.mandala.service.JsonResult;
import com.mandala.service.OdmUserRepo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/permission")
@Api("系统权限查询接口")
public class PermissionController {
	@Autowired
	private OdmUserRepo odmUserRepo;
	@Autowired
	private JsonResult jsonResult;
	@Autowired
	private LDAPConnectService connectService;
	
	@ApiOperation(value = "根据权限id", notes = "权限查询")
    @ApiImplicitParam(name = "ou", value = "权限ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getPermissionByOu/{ou}" , method = RequestMethod.GET)
    public  ResponseWrapper getPermissionByOu(@PathVariable("ou") String ou) {
		ResponseWrapper response = new ResponseWrapper();
		if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(ou.equals("null") || ou ==null){
    		return jsonResult.markParamError();
    	}
		Permission permission = new Permission();
		permission = odmUserRepo.findPermissionByOu(ou);
		if(permission != null){
			response = jsonResult.markSuccess(permission);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
	
	@ApiOperation(value = "根据系统的id", notes = "权限信息查询")
    @ApiImplicitParam(name = "systemId", value = "系统ID", paramType = "path", required = true, dataType = "String")
    @RequestMapping(value = "/getPermissionBySystemId/{systemId}" , method = RequestMethod.GET)
    public  ResponseWrapper getPermissionBySystemId(@PathVariable("systemId") String systemId) {
		ResponseWrapper response = new ResponseWrapper();
		if(!connectService.connectLdap()){
			return jsonResult.markConnectError();
		}
    	if(systemId.equals("null") || systemId ==null){
    		return jsonResult.markParamError();
    	}
		List<Permission> permissionList = new ArrayList<>();
		permissionList = odmUserRepo.findPermissionBySystemId(systemId);
		if(permissionList.size() > 0){
			response = jsonResult.markSuccess(permissionList);
        }else{
        	response = jsonResult.markSuccessButNoData();
        }
        return response;
    }
    
    
}